<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                        <!-- <el-form-item label="会话ID" prop="conversationId">
                            <el-input
                                    v-model="queryParams.conversationId"
                                    placeholder="请输入会话ID"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
                        <el-form-item label="用户标识" prop="userId">
                            <el-input
                                    v-model="queryParams.userId"
                                    placeholder="请输入用户标识"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="创建日期" prop="createdTime">
                            <el-date-picker clearable
                                            v-model="queryParams.createdTime"
                                            type="date"
                                            value-format="YYYY-MM-DD"
                                            placeholder="请选择创建日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="会话标题" prop="conversationTitle">
                            <el-input
                                    v-model="queryParams.conversationTitle"
                                    placeholder="请输入会话标题"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['bus:AIChat:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['bus:AIChat:edit']"
                >修改</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['bus:AIChat:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['bus:AIChat:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="AIChatList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="消息唯一标识" align="center" prop="messageId" />
                    <el-table-column label="会话ID" align="center" prop="conversationId" />
                    <el-table-column label="用户标识" align="center" prop="userId" />
                    <!-- <el-table-column label="用户消息内容" align="center" prop="usercontent" /> -->
                    <!-- <el-table-column label="AI消息内容" align="center" prop="syscontent" /> -->
                    <el-table-column label="创建时间" prop="createdTime">
  <template #default="{ row }">
    {{ dayjs(row.createdTime).format('YYYY-MM-DD') }}
  </template>
</el-table-column>
                    <!-- <el-table-column label="会话标题" align="center" prop="conversationTitle" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看详情</el-button>
                    <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:AIChat:edit']">修改</el-button> -->
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:AIChat:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改消息对话框 -->
        <!-- <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="AIChatRef" :model="form" :rules="rules" label-width="80px">
                                <el-form-item label="会话ID" prop="conversationId">
                                    <el-input v-model="form.conversationId" placeholder="请输入会话ID" />
                                </el-form-item>
                                <el-form-item label="用户标识" prop="userId">
                                    <el-input v-model="form.userId" placeholder="请输入用户标识" />
                                </el-form-item>
                                <el-form-item label="用户消息内容">
                                    <editor v-model="form.usercontent" :min-height="192"/>
                                </el-form-item>
                                <el-form-item label="AI消息内容">
                                    <editor v-model="form.syscontent" :min-height="192"/>
                                </el-form-item>
                                <el-form-item label="精确到毫秒的创建时间" prop="createdTime">
                                    <el-date-picker clearable
                                                    v-model="form.createdTime"
                                                    type="date"
                                                    value-format="YYYY-MM-DD"
                                                    placeholder="请选择精确到毫秒的创建时间">
                                    </el-date-picker>
                                </el-form-item>
                                <el-form-item label="自动生成的会话标题" prop="conversationTitle">
                                    <el-input v-model="form.conversationTitle" placeholder="请输入自动生成的会话标题" />
                                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog> -->

        <!-- 查看详情对话框 -->
        <el-dialog title="消息详情" v-model="detailOpen" width="700px" append-to-body>
          <el-descriptions :column="1" border>
            <el-descriptions-item label="会话标题">
              {{ detailData.conversationTitle }}
            </el-descriptions-item>
            <el-descriptions-item label="用户消息内容">
              <div class="rich" v-html="detailData.usercontent"></div>
            </el-descriptions-item>
            <el-descriptions-item label="AI消息内容">
              <div class="rich" v-html="detailData.syscontent"></div>
            </el-descriptions-item>
          </el-descriptions>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="detailOpen = false">关 闭</el-button>
            </div>
          </template>
        </el-dialog>
    </div>
</template>

<script setup name="AIChat">
    import { listAIChat, getAIChat, delAIChat, addAIChat, updateAIChat } from "@/api/teenage/AIChat";
    import dayjs from 'dayjs';

    const { proxy } = getCurrentInstance();

    const AIChatList = ref([]);
    const open = ref(false);
    const detailOpen = ref(false);
    const loading = ref(true);
    const showSearch = ref(true);
    const ids = ref([]);
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);
    const title = ref("");
    const detailData = ref({});

    const data = reactive({
        form: {},
        queryParams: {
            pageNum: 1,
            pageSize: 10,
                conversationId: null,
                userId: null,
                usercontent: null,
                syscontent: null,
                createdTime: null,
                conversationTitle: null
    },
    rules: {
    }
    });

    const { queryParams, form, rules } = toRefs(data);

    /** 查询消息列表 */
    function getList() {
        loading.value = true;
        const query = {
            ...queryParams.value,
            createdDate: queryParams.value.createdTime || null,
        };
        delete query.createdTime;
        listAIChat(query).then(response => {
                AIChatList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }

    // 取消按钮
    function cancel() {
        open.value = false;
        reset();
    }

    // 表单重置
    function reset() {
        form.value = {
                messageId: null,
                conversationId: null,
                userId: null,
                usercontent: null,
                syscontent: null,
                createdTime: null,
                conversationTitle: null
    };
        proxy.resetForm("AIChatRef");
    }

    /** 搜索按钮操作 */
    function handleQuery() {
        queryParams.value.pageNum = 1;
        getList();
    }

    /** 重置按钮操作 */
    function resetQuery() {
        proxy.resetForm("queryRef");
        handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.messageId);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    /** 新增按钮操作 */
    function handleAdd() {
        reset();
        open.value = true;
        title.value = "添加消息";
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
        reset();
        const _messageId = row.messageId || ids.value
        getAIChat(_messageId).then(response => {
            form.value = response.data;
            open.value = true;
            title.value = "修改消息";
        });
    }

    /** 查看详情按钮操作 */
    function handleView(row) {
        const _messageId = row.messageId || ids.value;
        getAIChat(_messageId).then(response => {
            detailData.value = response.data || {};
            detailOpen.value = true;
        });
    }

    /** 提交按钮 */
    function submitForm() {
        proxy.$refs["AIChatRef"].validate(valid => {
            if (valid) {
                if (form.value.messageId != null) {
                    updateAIChat(form.value).then(response => {
                        proxy.$modal.msgSuccess("修改成功");
                        open.value = false;
                        getList();
                    });
                } else {
                    addAIChat(form.value).then(response => {
                        proxy.$modal.msgSuccess("新增成功");
                        open.value = false;
                        getList();
                    });
                }
            }
        });
    }

    /** 删除按钮操作 */
    function handleDelete(row) {
        const _messageIds = row.messageId || ids.value;
        proxy.$modal.confirm('是否确认删除消息编号为"' + _messageIds + '"的数据项？').then(function() {
            return delAIChat(_messageIds);
        }).then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    }

    /** 导出按钮操作 */
    function handleExport() {
        proxy.download('bus/AIChat/export', {
            ...queryParams.value
        }, `AIChat_${new Date().getTime()}.xlsx`)
    }

    getList();
</script>